import { defineNavbarConfig } from 'vuepress-theme-plume'
import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'

/**
 * 从 Markdown 文件中读取标题
 * @param filePath 
 * @returns 
 */
const readTitleFromMarkdown = (filePath: string) => {
  const fileContent = fs.readFileSync(filePath, 'utf-8')
  const { data } = matter(fileContent)
  console.log(data);
  
  return data.title || 'Untitled'
}

const notesDir = path.join(__dirname, '../notes')

/**
 * 获取notes下目录节点
 */
const noteItems = fs.existsSync(notesDir)
  ? fs.readdirSync(notesDir, { withFileTypes: true })
    .filter(dirent => dirent.isDirectory())
    .map(dirent => {
      const readmePath = path.join(notesDir, dirent.name, 'README.md')
      const title = fs.existsSync(readmePath) ? readTitleFromMarkdown(readmePath) : dirent.name
      return {
        text: title.toLowerCase(),
        link: `/notes/${dirent.name}/README.md`
      }
    })
  : []

console.log('noteItems',noteItems);


export const navbar = defineNavbarConfig([
  { text: '首页', link: '/' },
  { text: '博客', link: '/blog/' },
  {
    text: '笔记',
    items: noteItems
  },
  { text: '标签', link: '/blog/tags/' },
  { text: '归档', link: '/blog/archives/' },
])
